package com.x.Part3_Date;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

/**
 * ClassName: DateTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.04.04 22:14
 */
public class DateTest {

    @Test
    public void test1(){
        Date date1 =new Date();
        System.out.println(date1);

        long Times = date1.getTime();
        System.out.println(Times);

        Date date2 = new Date(991112143434L);
        System.out.println(date2);
    }

    @Test
    public void test2(){
        Scanner sc=new Scanner(System.in);
        long a=sc.nextLong();

        Date date1 = new Date(a-(8*60*60*1000));
        //对date1指定字符串格式
        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
        String dateStr = formatter.format(date1);
        System.out.println(dateStr);
    }

    @Test
    public void test3() throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //yy：年份
        //MM：月份
        //mm：分钟
        //dd：月份中的天数
        //DD：一年的天数
        //HH：24小时制小时
        //hh：12小时制小时

        Date date = new Date();
        String dateStr = formatter.format(date);//传入现在时刻
        System.out.println(dateStr);//指定输出格式： "yyyy-MM-dd HH:mm:ss"

        Date date1 = formatter.parse("2023-01-02 13:23:45");//解析对应格式的字符串，返回Date类对象
        System.out.println(date1);//Date类对象默认toString输出
    }

    @Test
    public void test4() {
        Calendar calendar = Calendar.getInstance();//Calendar是抽象类，实例化其子类的对象
        System.out.println(calendar.getClass());//GregorianCalendar为Calendar子类

        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//本月第几天
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//本年第几天

        //设置日期
        calendar.set(Calendar.DAY_OF_MONTH, 3);//设置为本月3号
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));

        //加减天数
        calendar.add(Calendar.DAY_OF_MONTH, -2);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//变成8号

        //getTime方法返回Date类对象
        Date date1 = calendar.getTime();
        System.out.println(date1);

        //setTime方法传入Date对象，重置calendar
        Date date2 = new Date();
        calendar.setTime(date2);//重置为现在的时间
        System.out.println(date2);
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//目前周几(如果周五则显示6)


    }
}
